package lumis.service.wsrp.producer;

import java.util.List;

import javax.persistence.EntityManager;

import lumis.doui.service.DouiService;
import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.serviceinstance.ServiceInstanceConfig;
import lumis.util.ITransaction;

import com.sun.portal.wsrp.producer.Producer;
import com.sun.portal.wsrp.producer.ProducerException;
import com.sun.portal.wsrp.producer.ProducerManager;

/**
 * Service that produces remote portlets using WSRP. 
 *
 * @version $Revision: 11180 $ $Date: 2009-12-04 18:13:02 -0200 (Fri, 04 Dec 2009) $
 * @since 5.5.0
 */
public class ProducerService extends DouiService
{
	@Override
	public void instanceDeleted(SessionConfig sessionConfig, ServiceInstanceConfig config, ITransaction transaction) throws PortalException
	{	
		super.instanceDeleted(sessionConfig, config, transaction);
		
		try
		{
			ProducerManager manager = ProducerManagerFactory.getProducerManager();
			List<Producer> producers = manager.getAllProducers(config.getId());
			EntityManager entityManager = ManagerFactory.getEntityManager();
			for (Producer producer: producers)
			{
				ProducerImpl producerImpl = entityManager.getReference(ProducerImpl.class, ((ProducerImpl)producer).getId());
				entityManager.remove(producerImpl);
			}
		}
		catch (ProducerException e)
		{
			throw new PortalException(e.getMessage(), e);
		}
	}

}
